#ifndef __TDP2_ERROR_HPP__
#define __TDP2_ERROR_HPP__

#include <setjmp.h>

// TDP2 can operate with exception handling disabled.  All errors that halt normal operation are thrown with TDP::Panic.
namespace TDP
{
	namespace FatalErrors
	{
		enum ErrorCode
		{
			ERR_OK,

			ERR_AllocationFailed,
			ERR_ValueNotFound,
		};
	}

	class PanicHandler
	{
		jmp_buf jumpTarget;
		volatile FatalErrors::ErrorCode errorCode;

	public:
		inline void Panic(FatalErrors::ErrorCode errorCode)
		{
			this->errorCode = errorCode;
			longjmp(this->jumpTarget, 1);
		}

		inline jmp_buf &JumpTarget()
		{
			return this->jumpTarget;
		}

		inline FatalErrors::ErrorCode ErrorCode() const
		{
			return this->errorCode;
		}
	};

	struct FatalErrorException
	{
		FatalErrors::ErrorCode _errorCode;

	public:
		inline explicit FatalErrorException(FatalErrors::ErrorCode errorCode)
		{
			_errorCode = errorCode;
		}

		inline FatalErrors::ErrorCode ErrorCode() const
		{
			return _errorCode;
		}
	};

	void Panic(const PanicHandler *ph, FatalErrors::ErrorCode errCode);
}

#endif
